﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetAverage
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Project 1: Make a program with a loop that will allow the user to enter a series of numbers.
             * The user must type -0 to signal the end of the series.
             * Finaly display the average of all numbers
             * Display what the program will do
             * method getNumber
             * method addNum
             * loop untill -0
             * Display average */
            Int64 smallest = 0, biggest = 0, number = 1, average = 0, total = 0;
            int count = 0;
            print("This program will allow you to enter 1 number at a time\nas many times as you want until you type the number \"0\"\nIt will give you the higest and lowest number of the numbers you have typed\nNOT including the number 0, and the average of all.");
            //this while will loop the process of typing and checking the number until -99 is typed.
            while (number != 0)
            {
                //getNumber method will make sure a whole number is given
                getNumber(ref number);
                if (number != -0)
                {
                    //the ammount of numbers that have been entered
                    count++;
                    //replaceAnswer method will replace smallest and biggest if needed
                    replaceAnswer(ref smallest, ref biggest, number);
                    //calcAverage
                    calcAverage(ref average, ref total, ref count, number);
                }
            }
            //display numbers after typing -99, if no numbers where given, biggest and smallest will be "0".
            print("The Smallest number is: " + smallest + " and the largest is: " + biggest);
            print("The Average number is: " + average + " of " + count + " numbers given.");
        }// end Main
        private static void calcAverage(ref Int64 average, ref Int64 total, ref int count, Int64 number)
        {
            total = number + total;
            average = total / count;
        }
        static void replaceAnswer(ref Int64 smallest, ref Int64 biggest, Int64 number)
        {
            if (smallest == 0 && biggest == 0)
            {
                smallest = number;
                biggest = number;
            }
            else if (number < smallest) //brackets removed for a smaller printout
                smallest = number;
            else if (number > biggest) //brackets removed for a smaller printout
                biggest = number;
        }// end repleaceAnswer
        static void getNumber(ref Int64 number)
        {
            print("Type a number to be added to the database:");
            while (!Int64.TryParse(Console.ReadLine(), out number))
                print("Invalid Number, please provide a whole number.");
        }// end getNumber
        /* i just like to use this method for WireLine on every program */
        static void print(string p)
        {
            Console.WriteLine("\n{0}", p);
        }// end print
    }
}